package com.operator.Activitys.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;

/**
 * 项目名称：金泽云 手机app
 * 类描述：图片上传进度 自定义View
 * 创建人：张飞祥
 * 创建时间：2018/5/14 14:15
 * 修改人：张飞祥
 * 修改时间：2018/5/14 14:15
 * 修改备注：
 */
public class ProcessImageView extends android.support.v7.widget.AppCompatImageView {
    // 画笔
    private Paint mPaint;
    //全局上下文
    private Context mContext = null;
    //上传进度
    private int mProgress = 0;

    public ProcessImageView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public ProcessImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProcessImageView(Context context, AttributeSet attrs,
                            int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        mPaint = new Paint();
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setAntiAlias(true); // 消除锯齿
        mPaint.setStyle(Paint.Style.FILL);


        //画圆角 这么写
//        mPaint.setColor(Color.parseColor("#70000000"));// 半透明
//        canvas.drawRoundRect(new RectF(0,0,getWidth(),getHeight()- getHeight() * mProgress
//                        / 100), 20, 20, mPaint);

        //默认
        if (mProgress > 0 && mProgress < 100){

            mPaint.setColor(Color.parseColor("#70000000"));// 半透明
            canvas.drawRect(0, 0, getWidth(), getHeight()- getHeight() * mProgress
                    / 100, mPaint);

            mPaint.setColor(Color.parseColor("#00000000"));// 全透明
            canvas.drawRect(0, 0, getWidth(), getHeight()- getHeight() * mProgress
                    / 100, mPaint);

            mPaint.setTextSize(30);
            mPaint.setColor(Color.parseColor("#FFFFFF"));
            mPaint.setStrokeWidth(2);
            Rect rect = new Rect();
            mPaint.getTextBounds("100%", 0, "100%".length(), rect);// 确定文字的宽度
            canvas.drawText(mProgress + "%", getWidth() / 2 - rect.width() / 2,
                    getHeight() / 2, mPaint);
        }

    }

    public void setProgress(int progress) {
        this.mProgress = progress;
        postInvalidate();
    };

}